﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_ManageRole : System.Web.UI.Page
{
    DAL.DALRole dr = new DAL.DALRole();

    string errMsg = "";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridRole.DataSource = dr.GetAllRole(out errMsg);
            GridRole.DataBind();
        }
    }
    protected void btnSure_Click(object sender, EventArgs e)
    {
        string rolename = this.tbRootName.Text.Trim();
        string roleId = lblRoleId.Text;
        string message = "";
        lblRoleId.Text = "";
        tbRootName.Text = "";
        string Orolename = "";
        try
        {
            if (roleId == "")
            {
                if (dr.InsertRole(rolename, out errMsg))
                {
                    GridRole.DataSource = dr.GetAllRole(out errMsg);
                    GridRole.DataBind();
                    message = "添加角色名成功，请给角色分配权限!";
                }
                else
                {
                    message = "添加角色名失败！请检查是否存在相同的角色名!";
                }
            }
            else
            {
                if (dr.UpdateRole(roleId, rolename, out errMsg))
                {
                    GridRole.DataSource = dr.GetAllRole(out errMsg);
                    GridRole.DataBind();
                    message = "修改角色名成功";
                    //修改XML中的角色名
                }
                else
                {
                    message = "修改角色名失败！请检查是否存在相同的角色名！";
                }
            }
        }
        catch (Exception ex)
        {
            message = "添加角色名失败！失败信息：" + ex.Message.ToString();
        }

        Response.Write("<script>alert('" + message + "')</script>");

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        lblRoleId.Text = "";
        tbRootName.Text = "";
    }

    protected void gridRole_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string rolename = GridRole.DataKeys[e.NewSelectedIndex].Value.ToString();
        tbRootName.Text = rolename;
        lblRoleId.Text = rolename;
    }

    protected void gridRole_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string rolename = GridRole.DataKeys[e.RowIndex].Value.ToString();
        string message = "";
        if (dr.DeleteRoleByNM(rolename, out errMsg))
        {
            GridRole.DataSource = dr.GetAllRole(out errMsg);
            GridRole.DataBind();
            message = "删除角色名成功";
        }
        else
        {
            message = "修改角色名失败！请检查是否存在相同的角色名！";
        }
        Response.Write("<script>alert('" + message + "')</script>");
    }
}
